# -*- coding: utf-8 -*-

import Plugin
import time
import urllib2
import os
import gtk
import threading

VERSION = '1.0'


class MainClass( Plugin.Plugin ):
    '''Main plugin class'''
    
    def __init__( self, controller, msn ):
        '''Contructor'''
        
        Plugin.Plugin.__init__( self, controller, msn )
        
        self.description = _('flooder beta')
        self.authors = { 'Bewiwi' : 'ghostloic@hotmail.com' }
        self.website = ''
        self.displayName = _('Flooder-MSN')
        self.name = 'Flooder'
        self.Slash = controller.Slash
        self.config = self.controller.config
        self.flooder = self.config.getPluginValue(self.name, 'flood', '10')
        self.tempo = self.config.getPluginValue(self.name, 'flood2', '2')
        self.text = self.config.getPluginValue(self.name, 'flood3', 'Ceci est du flood')   
        self.controller = controller 
        self.config.readPluginConfig(self.name)
        self.enabled = False
        
        
    def start( self ):
        '''start the plugin'''
        self.Slash.register('flood', self.flood, 'Utilisation\n      MODE DEFAULT:\n/flood 0\nflood utilise les parametre par defaut\n\n      MODE Dynamique:\n /flood a b c\na = nb d\'occurence\nb=tempo\nc=msg sans espace')
        self.Slash.register('floodf', self.floodf, 'FLOOD-FORCE\nUtilisation\n      MODE DEFAULT:\n/flood 0\nflood utilise les parametre par defaut\n\n      MODE Dynamique:\n /flood a b c\na = nb d\'occurence\nb=tempo\nc=msg sans espace')
        self.config.readPluginConfig(self.name)
        self.enabled = True
        	
        
    def stop( self ):    
        '''stop the plugin'''
        self.enabled = False
        self.Slash.unregister('flood')
        self.Slash.unregister('floodf')
          
    def check(self):
        return (True, 'Ok')
        
    def flood (self, slash_action):
    	data = slash_action.getParams()
    	z = int(data.split(" ")[0])
    	self.nb0 = 0
    	if z == 0:
	    	b = int(self.flooder)
	    	tempo = float(self.tempo)
	    	txt = str(self.text)
	else:
	    	b = int(data.split(" ")[0])
	    	tempo = float(data.split(" ")[1])
	    	varcount = data.count(" ")+1
	    	counter = 2
	    	txt = ""
	    	while counter < varcount:
	    		txt = txt+" "+data.split(" ")[counter]
	    		counter += 1
	    				
	
    		self.MyTimer(tempo,txt,slash_action,b)
    		
    		
    def MyTimer(self,tempo,txt,slash_action,b):
    	threading.Timer(tempo, self.MyTimer, [tempo,txt,slash_action,b]).start()
    	
    	if self.nb0 < b:
    		slash_action.outputText( txt , True)
    		self.nb0 += 1
    	else:
    		return 0
    		
    		
    def floodf (self, slash_action):
    	data = slash_action.getParams()
    	z = int(data.split(" ")[0])
    	a = 0
    	if z == 0:
	    	b = int(self.flooder)
	    	c = float(self.tempo)
	    	txt = str(self.text)
	else:
	    	b = int(data.split(" ")[0])
	    	c = float(data.split(" ")[1])
	    	varcount = data.count(" ")+1
	    	counter = 2
	    	txt = ""
	    	while counter < varcount:
	    		txt = txt+" "+data.split(" ")[counter]
	    		counter += 1
	    				
	
	while a < b:
    		slash_action.outputText( txt , True)
    		time.sleep(c)
    		a += 1
    		
    	
   
    		
    def configure(self):
    	'''config du plug'''
    	self.confW = Plugin.ConfigWindow(_('Config par default de Flood'),'')
    	lblPath = gtk.Label(_('Nombre d\'envoi'))
    	lblPath1 = gtk.Label(_('duré entre chaque envoi'))
    	lblPath2 = gtk.Label(_('texte'))
        txtPath = gtk.Entry()
        txtPath1 = gtk.Entry()
        txtPath2 = gtk.Entry()
        txtPath.set_text(self.flooder)
        txtPath1.set_text(self.tempo)
        txtPath2.set_text(self.text)
        
        self.confW.vbox.pack_start(lblPath)
        self.confW.vbox.pack_start(txtPath)
        self.confW.vbox.pack_start(lblPath1)
        self.confW.vbox.pack_start(txtPath1)
        self.confW.vbox.pack_start(lblPath2)
        self.confW.vbox.pack_start(txtPath2)
        self.confW.show_all()
        
	r = self.confW.run()

	if r is not None:
		self.config.setPluginValue( self.name, 'flood', txtPath.get_text() )
		self.config.setPluginValue( self.name, 'flood2', txtPath1.get_text() )
		self.config.setPluginValue( self.name, 'flood3', txtPath2.get_text() )
		self.stop()
            	self.start()


